package com.nhjf.dao.followThePainting;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.nhjf.dao.BaseDao;
import com.nhjf.model.followThePainting.FollowThePainting;
import com.nhjf.pojo.qo.followThePainting.FollowThePaintingQo;
@Repository
public class FollowThePaintingDao extends BaseDao<FollowThePainting, FollowThePaintingQo>{

	@Override
	protected Criteria buildCriteria(Criteria criteria, FollowThePaintingQo qo) {
		// TODO Auto-generated method stub
		if(qo!=null) {
			criteria.setFetchMode("uploader", qo.isSeachTeacher()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("typeOne", qo.isSeachTypeOne()?FetchMode.JOIN:FetchMode.SELECT);	
			criteria.setFetchMode("typeTwo", qo.isSeachTypeTwo()?FetchMode.JOIN:FetchMode.SELECT);	
//			criteria.setFetchMode("followThePaintingDetailsList", qo.isSeachFollowThePaintingDetails()?FetchMode.JOIN:FetchMode.SELECT);	
			if(StringUtils.isNotBlank(qo.getCover())) {
				criteria.add(Restrictions.eq("cover",qo.getCover()));
			}
			if(qo.getCreationTime()!=null) {
				criteria.add(Restrictions.eq("creationTime",qo.getCreationTime()));
			}
			if(StringUtils.isNotBlank(qo.getFtsTitle())) {
				criteria.add(Restrictions.like("ftsTitle",qo.getFtsTitle(),MatchMode.ANYWHERE));
			}
			if(qo.getPopularity()!=null) {
				criteria.add(Restrictions.eq("popularity",qo.getPopularity()));
			}
			if(qo.getFollowThePaintingId()!=null) {
				criteria.add(Restrictions.eq("id",qo.getFollowThePaintingId()));
			}
			if(qo.getUploader()!=null) {
				criteria.add(Restrictions.eq("uploader.id",qo.getUploader()));
			}
			if(qo.getTypeOne()!=null) {
				criteria.createAlias("typeOne", "typeOne");
				criteria.add(Restrictions.eq("typeOne.id",qo.getTypeOne()));
			}
			if(qo.getTypeTwo()!=null) {
				criteria.createAlias("typeTwo", "typeTwo");
				criteria.add(Restrictions.eq("typeTwo.id",qo.getTypeTwo()));
			}
			if(qo.getWidth()!=null) {
				criteria.add(Restrictions.eq("width",qo.getWidth()));
			}
			if(qo.getHeight()!=null) {
				criteria.add(Restrictions.eq("height",qo.getHeight()));
			}
			criteria.addOrder(Order.desc("creationTime"));
		}
		
		return criteria;
	}

	@Override
	protected Class<FollowThePainting> getEntityClass() {
		// TODO Auto-generated method stub
		return FollowThePainting.class;
	}

}
